package com.example.demo.common.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by zhangws on 2019/7/15 16:06.
 * 32小写英文字母
 */
public class Md5Util {
    static String[] chars = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};

    /**
     * md5 加密
     * @param src 需的加密字符串
     * @return
     */
    public static String getMd5(String src){
        MessageDigest md5 = null;
        try {
            // 参数代表的是算法名称
            md5 = MessageDigest.getInstance("md5");
            byte[] result = md5.digest(src.getBytes());

            StringBuilder sb = new StringBuilder(32);
            // 将结果转为16进制字符  0~9 A~F
            for (int i = 0; i < result.length; i++) {
                // 一个字节对应两个字符
                byte x = result[i];
                // 取得高位
                int h = 0x0f & (x >>> 4);
                // 取得低位
                int l = 0x0f & x;
                sb.append(chars[h]).append(chars[l]);
            }
            return sb.toString();

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        String str = "FffNtYLAWJcBPSopqu6v3DvpP6VXztZxcCj9hOkAagoR9wre5tV7ccKWcX3hOWYvqqLQxRYWnVAVIxi037oEmWaWHBGPzPs9P1sPZ8QJEwR44m5e3JtlWSYDkz+i14GoiHgYIHON/emwAcGqIEIuO2ipcWaHnibZ7+sx8ua+kHkGtaoCArkfyghis/7MCRm2CqKYzUNDSf7I7vqu3yzJXe+5oYDqZ8fTq1h1J5gdgAMykR5RgsLNdQIpOoz55yGLDTD4JFQ5IY7GraO4nYZulEILxjrBARDWZC49POQJu2E8ti+Z8F126CI38hK5uS8KrSFd5OcDUH9ih4FrY0wB+g==&Timestamp=1569312799137&AppKey=lx0d6ca761c31e5c51d2&Version=1.0&SecretKey=8c90fec8b592abe1d76ff771caa80225";
        System.out.println(getMd5(str).equals("c63061856e23b380b0fe6cd33ba1882e"));
    }
}
